<template>
	<view>
		<view class="search-box">
			<view class="acea-row row-middle search-bg">
				<!-- <image src="@/static/images/search_icon.png" mode=""></image> -->
				<text class="iconfont icon-xiazai5"></text>
				<input type="text" placeholder="搜索姓名/电话" placeholder-style="color:#a6a6a6;" v-model="searchKey" @confirm="getList">
			</view>
		</view>
		<view class="list">
			<view class="item" v-for="(item,index) in list" :key="item.id">
				<view class="acea-row row-middle row-between" @click="into_singleChatRoom(item)">
					<view class="acea-row  item-l">
						<view class="name"  >联系人：{{item.username}} </view>
						<!-- <view class="pd-6">
							<image src="../static/images/contact_03.png" mode=""></image>
						</view>	 -->
					</view>
					<view class="item-r phone">
						<view v-for="ph in item.phone?item.phone.split(','):[]" :key="ph">
							{{ph}}
						</view>
						
					</view>
				</view>
				<view class="info">
					<view class="acea-row row-top">
						<view class="label">
							备注：
						</view>
						<view class="text">
							{{item.remark || ''}}
						</view>
					</view>
							
					<view class="acea-row row-top">
						<view class="label">
							身份：
						</view>
						<view class="text">
							{{typeNames[item.typeName]}}
						</view>
					</view>
					<view class="acea-row row-top">
						<view class="label">
							关联信息：
						</view>
						<view class="text">
							<view class="" v-for="(info,index) in (item.information?item.information.split(','):[])" :key="index">{{info}}</view>
							<!-- <view class="">二手闲置|二手办公桌椅急售</view> -->
						</view>
					</view>
				</view>
				<view class="acea-row row-right row-middle btns">
					<!-- <view class="acea-row row-middle btn">
						<view class="pd-6">
							<image src="../static/images/contact_01.png" mode=""></image>
						</view>
						<view class="" @click="editContact(item)">编辑</view>
					</view> -->
					<view class="acea-row row-middle btn">
						<view class="pd-6">
							<image src="../static/images/contact_02.png" mode=""></image>
						</view>
						<view class="" @click="toDelete(item.id)">删除</view>
					</view>
				</view>
			</view>
		</view>
		<view class='loadingicon acea-row row-center row-middle'>
			<text class='loading iconfont icon-jiazai' :hidden='loading'></text>{{loadTitle}}
		</view>
		<view class="uni-p-b-98"></view>
		<view class="add-btn" @click="addContact">新增联系人</view>

	</view>
</template>

<script>
	import {
		toPage
	} from '@/filters/commFilter.js'
	import {
		addUserContacts,
		UserContactsOnList,
		UserContactsDel
	} from '@/api/user.js'
	export default {
		data() {
			return {
				searchKey: '',
				categorys: ['经营信息', '求租', '求购', '招工', '服务', '二手', '供应', '求职'],
				categoryOn: 0,
				page: 1,
				limit: 8,
				loading: true,
				loadTitle: '',
				typeNames:{
					1:'房东',2:'老板',3:'员工'
				},
				list:[]
			};
		},
		created() {
			this.getList()
		},
		onShow() {
			this.getList()
		},
		mounted() {
			let that = this;
			uni.$on('contact', function(data) {
				that.page = 1;
				// that.toAddUserContacts(data.id)
				that.getList();
			})
			uni.$on('upContact', function(data) {
				that.page = 1;
				that.getList();
			})
			uni.$on('initContact', function(data) {
				if (that.contactList&&that.contactList.length < 1) {
					that.contactList.push(data)
				}
			})
			getApp().globalData.initContact()
		},
		methods: {
			toPage,
			longClick(index) {
				if (this.categorys.length >= 4) {
					var tempIndex = index - 2;
					tempIndex = tempIndex <= 0 ? 0 : tempIndex;
					this.tabLeft = (index - 2) * this.isWidth //设置下划线位置
				}
				this.categoryOn = index;
				this.isLeft = index * this.isWidth //设置下划线位置
				this.list = []
				this.getList();
			},
			async getList() {
				this.loading = false;
				if(this.page == 1){
					this.list= []
				}
				try {
					let {
						data
					} = await UserContactsOnList({
						page: this.page,
						limit: this.limit,
						username: this.searchKey
						// type: this.categoryOn
					});
					// this.list = data.list
					this.loading = true
					if (this.page == 1) {
						this.list = data.list;
						if (this.list.length <= 0) {
							console.log('list', this.list)
							this.loadTitle = '暂无数据'
						}
					} else {
						this.list = this.list.concat(data.list);
						if (data.list.length <= 0) {
							this.loadTitle = '已加载所有'
						}
					}
				} catch (e) {
					//TODO handle the exception
					console.log(123, e)
				}
			},
			addContact() {
				uni.navigateTo({
					url: '/pages/releases/addShopContact/addShopContact?usercontact=true'
				})
			},
			editContact(data) {
				let d = {
					id: data.contactsId,
					remark: data.remark,
					phone: data.phone,
					username: data.username,
					type: data.typeName
				}
				// return;
				uni.navigateTo({
					url: '/pages/releases/addShopContact/addShopContact?info=' + JSON.stringify(d) 
				})
			},
			toDelete(id) {
				let that = this;
				uni.showModal({
					title: '提示',
					content: '你确定要删除吗',
					success: function(res) {
						if (res.confirm) {
							UserContactsDel(id).then(res => {
								that.page = 1;
								that.getList();
							})
						} else if (res.cancel) {
							console.log('取消');
						}
					}
				});
			},
			toAddUserContacts(id) {
				addUserContacts({
					contactsId: id
				}).then(res => {
					console.log(res)
				})
			},
			// 单聊
			into_singleChatRoom: function(item) {
				getApp().globalData.into_singleChatRoom({
					contactId:item.contactsId,
					phone:item.phone.split(',')[0]
				})
			},
		},
		onReachBottom() {
			this.page++;
			this.getList();
		}
	}
</script>

<style lang="scss">
	.pd-t-b-30 {
		padding: 0rpx 0 0;
	}

	.pd-6 {
		padding: 0 6rpx;
	}

	.mb-30 {
		margin-bottom: 30rpx;
	}

	.search-box {
		margin: 30rpx;
		border-radius: 10rpx;
		background: #ffffff;
		padding: 30rpx;

		.search-bg {
			width: 100%;
			height: 78rpx;
			background: #F5F5F5;
			border-radius: 39rpx;
			margin: auto;
			padding: 0 35rpx;

			.iconfont {
				font-size: 28rpx;
				color: rgba(166, 166, 166, 1);
			}

			image {
				display: block;
				width: 28rpx;
				height: 28rpx;
				margin: 0;
				margin-right: 15rpx;
			}

			input {
				display: block;
				width: calc(100% - 70rpx - 28rpx);
				font-size: 28rpx;
				padding-left: 12rpx;
			}
		}
	}

	.list {
		padding: 30rpx;

		.item {
			border-radius: 10rpx;
			background: #ffffff;
			padding: 30rpx;
			margin-bottom: 24rpx;

			image {
				width: 36rpx;
				height: 36rpx;
			}

			.item-r {
				width: 30%;
			}

			.item-l {
				width: 70%;
			}

			.name {
				font-size: 28rpx;
				font-weight: 400;
				line-height: 42rpx;
				color: rgba(56, 56, 56, 1);
			}

			.phone {
				font-size: 28rpx;
				font-weight: 500;
				line-height: 42rpx;
				color: rgba(0, 0, 0, 1.0);
				text-align: right;
			}
		}

		.info {
			padding: 30rpx 0;
			font-size: 26rpx;
			font-weight: 400;
			color: rgba(166, 166, 166, 1);

			.label {
				line-height: 42rpx;
			}

			.text {
				line-height: 42rpx;
				width: 70%;
			}
		}
	}

	.btns {
		padding-top: 30rpx;
		border-top: 1px solid rgba(238, 238, 238, 1);

		.btn {
			margin: 0 12rpx;
			font-size: 24rpx;
			font-weight: 400;
			line-height: 42rpx;
			color: rgba(128, 128, 128, 1);
		}
	}

	.navTabBox {
		background: #ffffff;
		padding: 24rpx 24rpx;

		.category-box {
			color: rgba(50, 50, 51, 1);
			font-size: 32rpx;
			font-weight: 500;

			.click {
				color: rgba(238, 33, 45, 1);
				border-bottom: 2px solid rgba(238, 33, 45, 1);
			}

			.longTab {
				width: calc(100%);
				/* #ifdef H5 */
				padding-bottom: 20rpx;
				/* #endif */
				/* #ifdef MP */
				padding-top: 12rpx;
				padding-bottom: 12rpx;

				/* #endif */
				.longItem {
					height: 50upx;
					display: inline-block;
					line-height: 50upx;
					text-align: center;
					font-size: 32rpx;
					margin: 0 20rpx;

					&.click {
						font-weight: bold;
					}
				}

				.underlineBox {
					height: 3px;
					width: 20%;
					display: flex;
					align-content: center;
					justify-content: center;
					transition: .5s;

					.underline {
						width: 33rpx;
						height: 4rpx;
						background-color: rgba(238, 33, 45, 1);
					}
				}
			}
		}

		.filtration {
			margin-left: 18rpx;
			padding: 12rpx 0 16rpx;

			.text {
				width: 80rpx;
				font-size: 28rpx;
				font-weight: 400;
				color: rgba(56, 56, 56, 1);
			}

			.sanjiao {
				width: 0;
				height: 0;
				border: 10rpx solid transparent;
				border-top-color: rgba(128, 128, 128, 1);
				border-bottom-width: 0;
			}
		}
	}

	.add-btn {
		width: 400rpx;
		height: 98rpx;
		opacity: 1;
		border-radius: 50rpx;
		background: rgba(238, 33, 45, 1);
		box-shadow: 0px 3px 8px 0px rgba(245, 15, 30, 0.2);
		position: fixed;
		bottom: 60rpx;
		left: 50%;
		transform: translateX(-50%);
		font-size: 36rpx;
		font-weight: 400;
		line-height: 98rpx;
		color: rgba(255, 250, 250, 1);
		text-align: center;
	}
</style>